package com.shuyuntu.proposal.controller;

import com.shuyuntu.proposal.security.UseSecurity;
import com.shuyuntu.proposal.service.SecondMotionService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * 提案附议的Service层
 * Created by limeng on 16-5-5.
 */
@Controller
@RequestMapping("/second_motion")
public class SecondMotionController {
    /**
     * 根据提案的id，分页查询当前提案的附议信息
     * @param proposalId 提案id
     * @param pageSize 分页大小
     * @param pageNo 分页页号（1起）
     * @return {state:true/false,list:[附议列表],pageCount:页数}
     */
    @RequestMapping(value = "/{proposalId}/{pageSize}/{pageNo}", method = RequestMethod.GET)
    @ResponseBody
    public Map<String, Object> getSecondMotionByProposalIdWithPagination(
            @PathVariable Integer proposalId,
            @PathVariable int pageSize,
            @PathVariable int pageNo) {

        Map<String, Object> resMap = new LinkedHashMap<>();

        try {
            StringBuffer rowCount = new StringBuffer();
            List list = secondMotionService.getSecondMotionByProposalIdWithPagination(
                    proposalId, pageSize, pageNo, rowCount);
            Integer pageCount = (Integer.parseInt(rowCount.toString()) - 1) / pageSize + 1;
            resMap.put("state", true);
            resMap.put("list", list);
            resMap.put("pageCount", pageCount);
        } catch (Exception e) {
            e.printStackTrace();
            resMap.put("state", false);
            resMap.put("result", "访问数据库时遇到问题！");
        }

        return resMap;
    }

    /**
     * 上传一个附议
     * @param reqMap 附议信息的jsonMap
     * @return jsonMap
     */
    @UseSecurity(category = "03")
    @RequestMapping(value = "/", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> postSecondMotion(@RequestBody Map<String, Object> reqMap,
                                                HttpSession session) {
        Integer proposalId = (Integer)reqMap.get("proposalId");
        String authorId = (reqMap.get("authorId")).toString();
        String content = (String)reqMap.get("content");

        int result = secondMotionService.publishSecondMotion(authorId, proposalId, content);
        Map<String, Object> resMap = new LinkedHashMap<>();
        if (result == 0) {
            resMap.put("state", true);
        } else if (result == 1) {
            reqMap.put("state", false);
            reqMap.put("result", "数据库操作发生异常");
        } else if (result == 2) {
            resMap.put("state", false);
            resMap.put("result", "不允许给自己的提案附议！");
        } else if (result == 3) {
            resMap.put("state", false);
            resMap.put("result", "您已经参加过附议,请修改或删除自己的附议");
        } else {
            resMap.put("state", false);
            resMap.put("result", "当前提案的状态不允许附议!");
        }

        return resMap;
    }

    /**
     * 修改一个附议
     * @param reqMap 附议信息的jsonMap
     * @return jsonMap
     */
    @RequestMapping(value = "/{motionId}", method = RequestMethod.PUT)
    @ResponseBody
    public Map<String, Object> updateSecondMotion(@RequestBody Map<String, Object> reqMap,
                                                  @PathVariable int motionId,
                                                  HttpSession session) {
        Map<String, Object> resMap = new LinkedHashMap<>();

        String content = (String)reqMap.get("content");
        String authorId = (String)session.getAttribute("loginUserId");
        int result = secondMotionService.updateSecondMotion(motionId, content, authorId);
        if (result == 0) {
            resMap.put("state", true);
        } else if (result == 1) {
            resMap.put("state", false);
            resMap.put("result", "数据库操作发生异常");
        } else if (result == 2) {
            resMap.put("state", false);
            resMap.put("result", "不允许修改别人的提案附议！");
        } else if (result == 3) {
            resMap.put("state", false);
            resMap.put("result", "不存在当前id的附议!");
        } else {
            resMap.put("state", false);
            resMap.put("result", "当前提案的状态不允许修改附议!");
        }

        return resMap;
    }

    /**
     * 删除某个附议
     * @param motionId 待删除附议的id
     * @param session inject
     * @return jsonMap
     */
    @ResponseBody
    @RequestMapping(value = "/{motionId}", method = RequestMethod.DELETE)
    @UseSecurity(category = "03")
    public Map<String, Object> deleteMotion(@PathVariable int motionId, HttpSession session) {
        Map<String, Object> resMap = new LinkedHashMap<>();
        String authorId = (String) session.getAttribute("loginUserId");

        int stateCode = secondMotionService.deleteMotion(motionId, authorId);

        switch (stateCode) {
            case 0:
                resMap.put("state", true);
                break;
            case 1:
                resMap.put("state", false);
                resMap.put("result", "不存在当前附议id");
                break;
            case 2:
                resMap.put("state", false);
                resMap.put("result", "数据库异常!");
                break;
            case 3:
                resMap.put("state", false);
                resMap.put("result", "该附议不是本用户写的");
            default:
                break;
        }

        return resMap;
    }

    /*****依赖注入↓*****/
    @Resource
    SecondMotionService secondMotionService;

    public SecondMotionService getSecondMotionService() {
        return secondMotionService;
    }

    public void setSecondMotionService(SecondMotionService secondMotionService) {
        this.secondMotionService = secondMotionService;
    }
}
